namespace TSL.Utility.Json
{
    using System;
    using System.Collections;
    using System.Text;

    public class HashtableConverter : IJavaScriptConverter
    {
        public HashtableConverter()
        {
            base.m_serializableTypes = new Type[] { typeof(Hashtable) };
            base.m_deserializableTypes = new Type[] { typeof(Hashtable) };
        }

        public override object Deserialize(IJavaScriptObject o, Type t)
        {
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                int num;
                if (!(o is JavaScriptArray))
                {
                    throw new NotSupportedException();
                }
                JavaScriptArray array = (JavaScriptArray) o;
                for (num = 0; num < array.Count; num++)
                {
                    if (!(array[num] is JavaScriptArray))
                    {
                        throw new NotSupportedException();
                    }
                }
                IDictionary dictionary = (IDictionary) Activator.CreateInstance(t);
                for (num = 0; num < array.Count; num++)
                {
                    JavaScriptArray array2 = (JavaScriptArray) array[num];
                    object key = JavaScriptDeserializer.Deserialize(array2[0], Type.GetType(((JavaScriptString) array2[2]).ToString()));
                    object obj3 = JavaScriptDeserializer.Deserialize(array2[1], Type.GetType(((JavaScriptString) array2[3]).ToString()));
                    dictionary.Add(key, obj3);
                }
                return dictionary;
            }
            if (!(o is JavaScriptObject))
            {
                throw new NotSupportedException();
            }
            JavaScriptObject obj4 = (JavaScriptObject) o;
            IDictionary dictionary2 = (IDictionary) Activator.CreateInstance(t);
            foreach (string str in obj4.Keys)
            {
                dictionary2.Add(str, JavaScriptDeserializer.Deserialize(obj4[str], typeof(string)));
            }
            return dictionary2;
        }

        public override string Serialize(object o)
        {
            StringBuilder sb = new StringBuilder();
            this.Serialize(o, sb);
            return sb.ToString();
        }

        public override void Serialize(object o, StringBuilder sb)
        {
            IDictionary dictionary = o as IDictionary;
            if (dictionary == null)
            {
                throw new NotSupportedException();
            }
            IDictionaryEnumerator enumerator = dictionary.GetEnumerator();
            enumerator.Reset();
            bool flag = true;
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                sb.Append("[");
            }
            else
            {
                sb.Append("{");
            }
            while (enumerator.MoveNext())
            {
                if (flag)
                {
                    flag = false;
                }
                else
                {
                    sb.Append(",");
                }
                if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
                {
                    sb.Append('[');
                    JavaScriptSerializer.Serialize(enumerator.Key, sb);
                    sb.Append(',');
                    JavaScriptSerializer.Serialize(enumerator.Value, sb);
                    sb.Append(',');
                    JavaScriptUtil.QuoteString(enumerator.Key.GetType().FullName, sb);
                    sb.Append(',');
                    JavaScriptUtil.QuoteString(enumerator.Value.GetType().FullName, sb);
                    sb.Append(']');
                }
                else
                {
                    if (enumerator.Key is string)
                    {
                        JavaScriptSerializer.Serialize(enumerator.Key, sb);
                    }
                    else if (enumerator.Key is bool)
                    {
                        JavaScriptSerializer.Serialize(((bool) enumerator.Key) ? "true" : "false", sb);
                    }
                    else
                    {
                        JavaScriptUtil.QuoteString(enumerator.Key.ToString(), sb);
                    }
                    sb.Append(':');
                    JavaScriptSerializer.Serialize(enumerator.Value, sb);
                }
            }
            if (!Utility.Settings.OldStyle.Contains("renderJsonCompliant"))
            {
                sb.Append("]");
            }
            else
            {
                sb.Append("}");
            }
        }
    }
}

